package wangjg.coco.actions;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.action.StatusLineContributionItem;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.WorkbenchWindow;
import org.eclipse.swt.SWT;

import wangjg.coco.common.BingTranslator;

/**
 * Our sample action implements workbench action delegate.
 * The action proxy will be created by the workbench and
 * shown in the UI. When the user tries to use the action,
 * this delegate will be created and execution will be 
 * delegated to it.
 * @see IWorkbenchWindowActionDelegate
 */
public class MainAction implements IWorkbenchWindowActionDelegate {
	private IWorkbenchWindow window;
	/**
	 * The constructor.
	 */
	public MainAction() {
	}

	/**
	 * The action has been activated. The argument of the
	 * method represents the 'real' action sitting
	 * in the workbench UI.
	 * @see IWorkbenchWindowActionDelegate#run
	 */
	public void run(IAction action) {
		ISelection selection = ActionContext.instance.getSelection();
		final String text = ((TextSelection) selection).getText();
		if(selection instanceof TextSelection){
			Runnable runnable = new Runnable() {
				public void run() {
					String result = new BingTranslator().en2cn(text);
					MessageDialog.openInformation(window.getShell(),"Translate(en->zh_cn)",result);
				}
			};
			Display.getDefault().asyncExec(runnable);
		}
		
//		WorkbenchWindow workbenchWindow = (WorkbenchWindow) PlatformUI.getWorkbench ().getActiveWorkbenchWindow();
//		workbenchWindow.setStatusLineVisible(true);
//		IActionBars bars = workbenchWindow.getActionBars();
//		IStatusLineManager lineManager = bars.getStatusLineManager();
//		
//		StatusLineContributionItem statusItem = new StatusLineContributionItem("TestStatus",50);
//		statusItem.setText("状态栏：测试11");
//		lineManager.add(statusItem);
//		
//		lineManager.setMessage("xxxxxxxx");
	}

	/**
	 * Selection in the workbench has been changed. We 
	 * can change the state of the 'real' action here
	 * if we want, but this can only happen after 
	 * the delegate has been created.
	 * @see IWorkbenchWindowActionDelegate#selectionChanged
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		ActionContext.instance.setAction(action);
		ActionContext.instance.setSelection(selection);
	}

	/**
	 * We can use this method to dispose of any system
	 * resources we previously allocated.
	 * @see IWorkbenchWindowActionDelegate#dispose
	 */
	public void dispose() {
	}

	/**
	 * We will cache window object in order to
	 * be able to provide parent shell for the message dialog.
	 * @see IWorkbenchWindowActionDelegate#init
	 */
	public void init(IWorkbenchWindow window) {
		this.window = window;
	}
}